package com.activenetwork.smlite.testcase.events.createEvents.basics;

import java.util.List;

import javax.xml.datatype.DatatypeConfigurationException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;

import com.activenetwork.schema.model.event.EventBasicType;
import com.activenetwork.smlite.page.event.EventDetailView.EventBasicSubview;
import com.activenetwork.smlite.snippet.EventsSnippet;
import com.activenetwork.smlite.testcase.BaseCase;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.RandomString;
import com.activenetwork.util.verification.gson.FailureAssertion;
import com.activenetwork.util.verification.gson.FailurePoint;

public class Basics003 extends BaseCase {
	static final Logger logger = LoggerFactory.getLogger(Basics003.class);
	
	@Test
	public void Vefiry_Event_Title_is_mandatory_field() throws DatatypeConfigurationException{
		loginBusiness.login();
		
		EventBasicSubview eventBasicSubview = EventsSnippet.gotoAddEventPage();
		
		
		logger.info("Verify empty title will fire validation");
		try{
			eventBasicSubview.nextStep();
		}catch(RuntimeException e){
			logger.info("Next step is not sourcing so throw exception");
		}
		
		List<WebElement> errors = webDriver.findElements(By.cssSelector(".nexus-errors div.alert.alert-error>div"));
		boolean found = false;
		for(WebElement error : errors){
			String content = error.getText().trim();
			if(content.contains("Event Title")){
				found = true;
				break;
			}
		}
		if(!found){
			EnvContext.failurePointTL.get().add(
					new FailurePoint(Basics003.class.getName(), "Verify empty event title validation", "Empty event title validation does not fire"));
		}
		
		logger.info("Navigate back to event and add event again");
		eventBasicSubview = EventsSnippet.gotoAddEventPage();
		
	
		logger.info("Verify the event title can only input 100 characters");
		EventBasicType eventBasic = new EventBasicType();
		
		logger.info("Set event title exceeds 100");
		String title = RandomString.generateReplicateString(20, "abcd 1234 ");
		eventBasic.setTitle(title);
		eventBasic.setCategory("Expo");

		eventBasicSubview.setEventTitle(eventBasic);
		eventBasicSubview.setEventCategory(eventBasic);
		
		WebElement h1 = webDriver.findElement(By.cssSelector("#nexus-mainui h1[data-smodel-path='event.eventName']"));
		String h1Content = h1.getText().trim();
		if(h1Content.length() > 100){
			EnvContext.failurePointTL.get().add(
					new FailurePoint(Basics003.class.getName(), "Verify event title max length 100", "Event title Maximum exceeds 100"));
		}
		
		// logout
		loginBusiness.logout();
		
		// Popup failures
		FailureAssertion.pop(Basics003.class);
	}
}
